import fitz

from app.utils.pymupdf_tools import detect_page_number_region
from tests.base_test import base_test_case

logger = base_test_case.get_logger(__name__)
TEST_DATA_DIR = base_test_case.test_data_dir
OUTPUT_DATA_DIR = base_test_case.output_data_dir
# input_pdf = TEST_DATA_DIR / "1711605374231.pdf"
input_pdf = TEST_DATA_DIR / "1715339805571.pdf"
# input_pdf = TEST_DATA_DIR / "1715340454803.pdf"
# input_pdf = TEST_DATA_DIR / "1715306099910.pdf"


def main():
    if not input_pdf.exists():
        raise FileNotFoundError(f"PDF not found: {input_pdf}")

    doc = fitz.open(input_pdf)
    try:
        # 自动识别页码区域
        rect = detect_page_number_region(doc)
        if rect:
            # 识别页码
            page_numbers = []
            for record in rect:
                print(f"Page {record.num}: page_index={record.page_index}, region={record.region}, alignment={record.alignment}, bbox={record.bbox}")
                page_numbers.append(record.num)
        else:
            logger.info("❌ 未检测到有效页码区域")
    finally:
        doc.close()


if __name__ == "__main__":
    # 已完成测试
    main()
